// ignore_for_file: deprecated_member_use

import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:url_launcher/url_launcher.dart';

class FlutterWebPage extends StatefulWidget {
  final String url;
  final String title;
  const FlutterWebPage({super.key, required this.title, required this.url});

  @override
  State<FlutterWebPage> createState() => _FlutterWebPageState();

    static pushRouter(BuildContext context, String url,{String title = "详情"}) async {
    return Navigator.push(
        context,
        MaterialPageRoute(
            builder: (BuildContext context) => FlutterWebPage(
                  title: title,
                  url:url
                )));
  }
}

class _FlutterWebPageState extends State<FlutterWebPage> {

 final GlobalKey webViewKey = GlobalKey();

  InAppWebViewController? webViewController;
  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(
          useShouldOverrideUrlLoading: true,
          mediaPlaybackRequiresUserGesture: false),
      android: AndroidInAppWebViewOptions(
        useHybridComposition: true,
      ),
      ios: IOSInAppWebViewOptions(
        allowsInlineMediaPlayback: true,
      ));

 @override
  Widget build(BuildContext context) {
    return (Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        leading: IconButton(
            icon: const Icon(Icons.arrow_back_ios, color: Colors.orange),
            onPressed: () async {
              Navigator.pop(context);
            }),
        centerTitle: true,
       // backgroundColor: ThemeColors.homemainColor, //标题居中显示
      ),
      body: InAppWebView(
        key: webViewKey,
        initialUrlRequest: URLRequest(url: Uri.parse(widget.url)),
        initialUserScripts: UnmodifiableListView<UserScript>([]),
        initialOptions: options,

        onWebViewCreated: (controller) {
          webViewController = controller;
        },
       onLoadStart: (controller, url) async {
       

       },
       
        onDownloadStart: (controller, url) async {
          final request = await Permission.storage.request();
          debugPrint("request=$request");
          // if (request == PermissionStatus.granted) {
          //   updateAppEx(url.path);
          // } else {
          //   print("授权成功失败-->$request");
     
          //   launch(url.path);
          // }
            launch(url.path);
        },
        androidOnPermissionRequest: (controller, origin, resources) async {
          return PermissionRequestResponse(
              resources: resources,
              action: PermissionRequestResponseAction.GRANT);
        },

      ),
    ));
  }

}